/**
 * Created by chen on 2017/5/15.
 */
//地图JS
var ISS_MAP = {
    version: '1.0'
};

//公共常量
ISS_MAP.nearByMap = '';

//无限滚动变量
ISS_MAP.sroll = {
    //点击第一次页面执行
    flag_order_tab1: true,
    //当前tab标签，0：门票 1：酒店 2：餐厅 3:购物 4：娱乐 5：停车场
    current_tabindex: 0,
    //定义每页的页数
    size: 10,
    //每个tab的当前页
    index_tab0: 1,
    index_tab1: 1,
    index_tab2: 1,
    //删除滚动后重新添加滚动
    flag_InfiniteScroll: true,
    //是否需要滚动加载
    flag_needScroll: true,
    //滚动事件只执行一次
    flag_execute: false
};

//有数据正在加载
ISS_MAP.Loading = false;

//查询内容
ISS_MAP.Keyword = "";

//地图初始化
ISS_MAP.Init = function () {
    //定义地图
    ISS_MAP.nearByMap = new BMap.Map("map-box", { enableMapClick: true });
    //启用滚轮放大缩
    ISS_MAP.nearByMap.enableScrollWheelZoom();
    //定义地图风格
    ISS_MAP.nearByMap.setMapStyle({ style: ISS_MAP_TYPE });
    //添加缩放控件
    var navigationControl = new BMap.NavigationControl({
        //靠左上角位置
        anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
        //LARGE类型
        type: BMAP_NAVIGATION_CONTROL_ZOOM
    });
    ISS_MAP.nearByMap.addControl(navigationControl);
    // 添加定位控件
    var geolocationControl = new BMap.GeolocationControl();
    geolocationControl.addEventListener("locationSuccess", function (e) {
        // 定位成功事件
        ISS_MAP_LOCAL_LNG = e.point.lng;
        ISS_MAP_LOCAL_LAT = e.point.lat;
    });
    geolocationControl.addEventListener("locationError", function (e) {
        // 定位失败事件
    });
    ISS_MAP.nearByMap.addControl(geolocationControl);
    //绑定地理位置
    var point = new BMap.Point(ISS_MAP_CENTER_LNG, ISS_MAP_CENTER_LAT);
    ISS_MAP.nearByMap.centerAndZoom(point, 16);
    //搜索栏
    var mySearchbar = myApp.searchbar('.searchbar', {
        searchList: '.list-block-search',
        searchIn: '.item-title',
        customSearch: true
    });

    ISS_MAP.nearByMap.addEventListener("click", function () {
        if ($$("#bottommenu_foot").html()) {
            $$("#bottommenu_foot").html("")
        }
    });

    //初始化进入
    ISS_MAP.PickModal();

    // $("#pickbar")[0].style.height = "42%";
    // $("#map-box")[0].style.height = "58%";

    //无限滚动 加载数据
    //第一次加载显示index_tab1，已付款的
    if (ISS_MAP.sroll.flag_order_tab1 == true) {
        //LoadAll("", ISS_MAP.sroll.index_tab0, ISS_MAP.sroll.current_tabindex);
        LocalGeocoder_yuqing();
        ISS_MAP.sroll.flag_order_tab1 = false;
    }
};

//设置地图中心店
ISS_MAP.SetCenter = function (lng, lat, zoom) {
    var point = new BMap.Point(lng, lat);
    ISS_MAP.nearByMap.centerAndZoom(point, zoom);
}

//Marker集合
ISS_MAP.Markers = [];

ISS_MAP.Points = [];



//绘点
ISS_MAP.DrawArc = function (longitude, latitude, data, currentIndex, index) {
    var point = new BMap.Point(longitude, latitude);

    if (ISS_MAP.sroll.current_tabindex == 0) {
        if (data.Distance == "") {
            data.Distance = distanceToMe(point);
        }
    } else {
        if (data.distance == "") {
            data.distance = distanceToMe(point);
        }
    }
    //图标

    ISS_MAP.Points.push(point);


    // var c_myIcon = new BMap.Icon("/Common/website/assets/images/images-yuqing/maps/common_poi@2x.png", new BMap.Size(32, 32));

    var c_myIcon = new BMap.Icon("../../assets/images/images-yuqing/maps/common_poi@2x.png", new BMap.Size(32, 32), {
        imageSize: new BMap.Size(32, 32)
    });
    var marker = new BMap.Marker(point, { icon: c_myIcon }); // 创建点
    ISS_MAP.Markers.push(marker); //添加到集合
    ISS_MAP.nearByMap.addOverlay(marker); //展示
    // ISS_MAP.nearByMap.setZoom(11);

    marker.addEventListener("click", function () {
        event.stopPropagation();
        var searchTemplate;
        switch (currentIndex) {
            case 0:
                searchTemplate = $$("#template_Ticket").html()
                break;
            case 1:
                searchTemplate = $$("#template_Hotel").html()
                break;
            case 2:
                searchTemplate = $$("#template_Rest").html()
                break;
            case 3:
                searchTemplate = $$("#template_entertainment").html()
                break;
            case 4:
                searchTemplate = $$("#template_park").html()
                break;
            case 5:
                searchTemplate = $$("#template_toilet").html()
                break;

        }

        if (searchTemplate) {
            var compiledTemplate = Template7.compile(searchTemplate);
            var html = compiledTemplate(data);
            //                        if (pageindex == 1) {
            //                            //清除内容
            //                            $$("#tab_Hotel .content-block").html("");
            //                        }

            //清除内容(修改，每次都清空）
            // $$("#bottommenu_foot").show();
            $$("#bottommenu_foot").html("");
            $$("#bottommenu_foot").append(html);





            $('.floating-button-yuqing').click(function () {

                event.stopPropagation();

                ISS_MAP_SEARCH_END_NAV.Lng = ISS_MAP.Markers[index].point.lng;
                ISS_MAP_SEARCH_END_NAV.Lat = ISS_MAP.Markers[index].point.lat;
                if (ISS_MAP.sroll.current_tabindex == 0) {
                    ISS_MAP_SEARCH_END_NAV.Name = data.CnName;
                } else if (ISS_MAP.sroll.current_tabindex == 4) {
                    ISS_MAP_SEARCH_END_NAV.Name = data.ShortName;
                } else {
                    ISS_MAP_SEARCH_END_NAV.Name = data.cnName;
                }
                var jsonstr1 = JSON.stringify(ISS_MAP_SEARCH_START_NAV);
                var jsonstr2 = JSON.stringify(ISS_MAP_SEARCH_END_NAV);
                // encodeURIComponent()转码
                window.location.href = '../map/navigationMap.html?map_start=' + encodeURIComponent(JSON.stringify(ISS_MAP_SEARCH_START_NAV)) + '&map_end=' + encodeURIComponent(JSON.stringify(ISS_MAP_SEARCH_END_NAV));
                // window.open();
            })
        }
    });



    //弹出PickModal
    // ISS_MAP.PickModal();
}

//跳转事件
ISS_MAP.MapGoto_Point = null;
ISS_MAP.MapGoto_Type = null;
ISS_MAP.MapGoto = function clickFunc() {
    var name = "未知";
    if (ISS_MAP.MapGoto_Type == "Toilet") {
        name = "公厕";
    } else if (ISS_MAP.MapGoto_Type == "Bike") {
        name = "自行车";
    } else if (ISS_MAP.MapGoto_Type == "Park") {
        name = "停车场";
    } else if (ISS_MAP.MapGoto_Type == "Hospital") {
        name = "医院";
    } else if (ISS_MAP.MapGoto_Type == "PassengerStation") {
        name = "长途汽车站";
    } else if (ISS_MAP.MapGoto_Type == "Logistics") {
        name = "物流快递";
    } else if (ISS_MAP.MapGoto_Type == "AutoMobile") {
        name = "汽修厂";
    } else if (ISS_MAP.MapGoto_Type == "Gymnasium") {
        name = "体育馆游乐园";
    } else if (ISS_MAP.MapGoto_Type == "Barbucue") {
        name = "烧烤夜市";
    } else if (ISS_MAP.MapGoto_Type == "Market") {
        name = "商场";
    } else if (ISS_MAP.MapGoto_Type == "Attrac") {
        name = "景点";
    } else if (ISS_MAP.MapGoto_Type == "Service") {
        name = "服务中心";
    }
    var param = "?name=" + name + "&lng=" + ISS_MAP.MapGoto_Point.lng + "&lat=" + ISS_MAP.MapGoto_Point.lat;
    var hrefstr = "/Common/website/view/map/IframeForGoto.html" + param;
    window.parent.MapClickJump(hrefstr, param);
}

ISS_MAP.DrawArcConst = function (points, paramFlag) {
    //图标
    var c_myIcon = "";
    if (paramFlag == "Toilet") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/toilet@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Bike") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/bicycle@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Park") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/park@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Hospital") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/hospital@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "PassengerStation") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/station@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Logistics") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/delivery@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "AutoMobile") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/repair@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Gymnasium") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/gym@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Barbucue") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/barbecue@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Market") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/supermark@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Attrac") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/spot@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Service") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/spot@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    }
    $.each(points, function (index, value) {
        var pointBD = new BMap.Point(value.lon, value.lat);
        //图标
        var marker = new BMap.Marker(pointBD, { icon: c_myIcon }); // 创建点
        ISS_MAP.nearByMap.addOverlay(marker); //展示
        ISS_MAP.MapGoto_Point = pointBD;
        ISS_MAP.MapGoto_Type = paramFlag;
        marker.addEventListener("click", ISS_MAP.MapGoto);
    });
    if (points && points.length > 0) {
        ISS_MAP.SetCenter(points[0].lon, points[0].lat, 13);
    }
}

ISS_MAP.DrawArcConst2 = function (points, paramFlag) {
    //图标
    var c_myIcon = "";
    if (paramFlag == "Toilet") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/toilet@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Bike") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/bicycle@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Park") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/park@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Hospital") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/hospital@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "PassengerStation") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/station@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Logistics") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/delivery@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "AutoMobile") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/repair@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Gymnasium") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/gym@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Barbucue") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/barbecue@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Market") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/supermark@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Attrac") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/spot@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    } else if (paramFlag == "Service") {
        c_myIcon = new BMap.Icon("/Common/website/assets/images/common/map/ConstMap/spot@2x.png", new BMap.Size(47, 54), {
            offset: new BMap.Size(25, 29),
        });
    }
    $.each(points, function (index, value) {
        var pointBD = new BMap.Point(value.lon, value.lat);
        //图标
        var marker = new BMap.Marker(pointBD, { icon: c_myIcon }); // 创建点
        ISS_MAP.nearByMap.addOverlay(marker); //展示
        ISS_MAP.MapGoto_Point = pointBD;
        ISS_MAP.MapGoto_Type = paramFlag;
        marker.addEventListener("click", ISS_MAP.MapGoto);
    });
}

//清除地图覆盖物
ISS_MAP.ClearArc = function () {
    ISS_MAP.nearByMap.clearOverlays();
}

////Preloader show
//ISS_MAP.ShowPre = function () {
//    $$("#preloader").css("display", "block");
//}

////Preloader hide
//ISS_MAP.HidePre = function () {
//    $$("#preloader").css("display", "none");
//}

//Pick Modal
ISS_MAP.PickModal = function () {
    myApp.pickerModal(".picker-1");
}

//framework7的弹出层
/* 变量 */
var map = "map/map_yuqing.html";

//地图导航
var contactsCallback_map = myApp.onPageInit('mapthree', function (page) {
    //初次加载的时候page为undefine,所以page不空的时候就是返回的,需要动态加载
    if (page) {
        //动态加载JS,只需重新绑定事件,方法可以直接调用
        var oHead = document.getElementsByTagName('HEAD').item(0);
        var oScript = document.createElement("script");
        oScript.type = "text/javascript";
        oScript.src = "../../assets/js/map/NearBy/Map_Tour_yuqing.js";
        oHead.appendChild(oScript);
        //标识ISS_MAP.sroll.flag_order_tab1,因为返回的时候要重新加载
        ISS_MAP.sroll.flag_order_tab1 = true;
    }
    var type = ISS.utils.getParameter("type");
    if (type != null && type != undefined && type != "") {
    } else {
        $$(".returnbox a i").removeClass("iconfont");
        $$(".returnbox a i").removeClass("icon-jiantou-left");
        $$(".returnbox a i").addClass("iconfont");
        $$(".returnbox a i").addClass("icon-shouye");
    }

    //返回按钮
    $("#returnbox").click(function () {
        if (type != null && type != undefined && type != "") {
            window.location.href = "../../view-yuqing/other/tour.html";
        } else {
            window.location.href = _home;
        }
    });

    $('.button').each(function (index, e) {
        $(this).click(function () {
            $('.button').removeClass("active");
            $(this).addClass("active");
            ISS_MAP.sroll.current_tabindex = index;
            if (ISS_MAP.sroll.current_tabindex != 4 && ISS_MAP.sroll.current_tabindex != 5) {
                $$(".menu_map").show();
            } else {
                $$(".menu_map").hide();
            }
            $$("#bottommenu_foot").html("");
            LoadAll_yuqing('', 1, ISS_MAP.sroll.current_tabindex);

        })
    });

    $$(".menu_map").on("click", function () {
        if (ISS_MAP.sroll.current_tabindex == 0) {
            mainView.router.loadPage({ url: "", reload: false });
            window.location.href = "../../view/booking/ticket.html?type=map";
        } else if (ISS_MAP.sroll.current_tabindex == 1) {
            window.location.href = "../../view/booking/hotel.html?type=map";
        } else if (ISS_MAP.sroll.current_tabindex == 2) {
            window.location.href = "../../view/elements/food.html?type=map";
        } else if (ISS_MAP.sroll.current_tabindex == 3) {
            window.location.href = "../../view/elements/entertainment.html?type=map";
            // mainView.router.loadPage({ url: "../../view/elements/entertainment.html?type=map", reload: false });
        }
    })


    //初始化
    if (type != null && type != undefined && type != "") {
        if (type == "park") {
            type = 4;
            $('.button').removeClass("active");
            $('.button.map_park').addClass("active");
            $$(".menu_map").hide();
            ISS_MAP.sroll.current_tabindex = type;
            $$("#bottommenu_foot").html("");
            // LoadAll_yuqing('', 1, ISS_MAP.sroll.current_tabindex);
            ISS_MAP.Init();
        } else if (type == "toilet") {
            type = 5;
            $('.button').removeClass("active");
            $('.button.map_toilet').addClass("active");
            $$(".menu_map").hide();
            ISS_MAP.sroll.current_tabindex = type;
            $$("#bottommenu_foot").html("");
            // LoadAll_yuqing('', 1, ISS_MAP.sroll.current_tabindex);
            ISS_MAP.Init();
        } else if (type == "service") {
            $('.button').removeClass("active");
            $$(".menu_map").hide();
            ISS_MAP.sroll.current_tabindex = -1;
            ISS_MAP.Init();
            ISS_MAP.DrawArcConst(ISS_Map_BaseElement_XX.Service(), "Service");
            // ElementClick();
        }
    } else {
        ISS_MAP.Init();
    }
});

/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(map) > -1) {
        contactsCallback_map.trigger();
    }
}

initData();




function distanceToMe(point) {

    var myPoint = new BMap.Point(ISS_MAP_SEARCH_START_NAV.Lng, ISS_MAP_SEARCH_START_NAV.Lat);

    var distance = ISS_MAP.nearByMap.getDistance(myPoint, point) / 1000;

    var pointIndex = String(distance).lastIndexOf(".");
    if (pointIndex > 0) {
        distance = String(distance).substring(0, pointIndex + 2);
    }
    return distance;
}






